<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="会员id" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['product:info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:info:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column
        label="累计消费金额"
        align="center"
        prop="consumeAmount"
      />
      <el-table-column
        label="累计优惠金额"
        align="center"
        prop="couponAmount"
      />
      <el-table-column label="订单数量" align="center" prop="orderCount" />
      <el-table-column label="优惠券数量" align="center" prop="couponCount" />
      <el-table-column label="评价数" align="center" prop="commentCount" />
      <el-table-column
        label="退货数量"
        align="center"
        prop="returnOrderCount"
      />
      <el-table-column
        label="收藏的商品数量"
        align="center"
        prop="collectProductCount"
      />
      <el-table-column label="积分" align="center" prop="integration" />
      <el-table-column label="成长值" align="center" prop="growth" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:info:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员统计信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会员id" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="累计消费金额" prop="consumeAmount">
          <el-input
            v-model="form.consumeAmount"
            placeholder="请输入累计消费金额"
          />
        </el-form-item>
        <el-form-item label="累计优惠金额" prop="couponAmount">
          <el-input
            v-model="form.couponAmount"
            placeholder="请输入累计优惠金额"
          />
        </el-form-item>
        <el-form-item label="订单数量" prop="orderCount">
          <el-input v-model="form.orderCount" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="优惠券数量" prop="couponCount">
          <el-input v-model="form.couponCount" placeholder="请输入优惠券数量" />
        </el-form-item>
        <el-form-item label="评价数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评价数" />
        </el-form-item>
        <el-form-item label="退货数量" prop="returnOrderCount">
          <el-input
            v-model="form.returnOrderCount"
            placeholder="请输入退货数量"
          />
        </el-form-item>
        <el-form-item label="收藏商品数" prop="collectProductCount">
          <el-input
            v-model="form.collectProductCount"
            placeholder="请输入收藏的商品数量"
          />
        </el-form-item>
        <el-form-item label="积分" prop="integration">
          <el-input v-model="form.integration" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="成长值" prop="growth">
          <el-input v-model="form.growth" placeholder="请输入成长值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listMemberStatisticsInfo,
  getMemberStatisticsInfo,
  delMemberStatisticsInfo,
  addMemberStatisticsInfo,
  updateMemberStatisticsInfo,
} from "@/api/mall/server/member/info";
const { proxy } = getCurrentInstance();
const list = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberId: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
const getList = async () => {
  loading.value = true;
  const res = await listMemberStatisticsInfo(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};
/** 表单重置 */
const reset = () => {
  form.value = {
    id: undefined,
    memberId: undefined,
    consumeAmount: undefined,
    couponAmount: undefined,
    orderCount: undefined,
    couponCount: undefined,
    commentCount: undefined,
    returnOrderCount: undefined,
    collectProductCount: undefined,
    integration: undefined,
    growth: undefined,
  };
  proxy.resetForm("formRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加会员信息";
};
/**修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const id = row.id || ids.value;
  const res = await getMemberStatisticsInfo(id);
  form.value = res.data;
  open.value = true;
  title.value = "修改会员信息";
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateMemberStatisticsInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMemberStatisticsInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + ids + '"的数据项？')
    .then(function () {
      return delMemberStatisticsInfo(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

getList();
</script>
